﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using AjaxControlToolkit;

namespace CoolCode.Web
{
    public static class Extensions
    {
        public static bool IsAjaxPage(this Page page)
        {
            return (ToolkitScriptManager.GetCurrent(page) != null);  
        }

        public static bool IsAjaxPage(this Control control)
        {
            return (ToolkitScriptManager.GetCurrent(control.Page) != null);
        }

        public static void Alert(this UpdatePanel panel, string message)
        {
            if (message.Length > 50)
            {
                message = message.Substring(0, 50);
            }
            //去除javascript不支持的字符
            message = Utility.ToScript(message);
            AjaxControlToolkit.ToolkitScriptManager.RegisterClientScriptBlock(panel, panel.GetType(), "Message",
                   string.Format( " alert('{0}'); ", message) , true);
        }

        public static void Alert(this UpdatePanel panel, Exception  exception)
        {
            Alert(panel, exception.Message);
        }

        public static void Alert(this UpdatePanel panel, string message, Exception exception)
        {
            message += exception.Message;
            Alert(panel, message );
        }

        //public static string ToMessage(this NoBotState state)
        //{
        //    string errorCode = string.Empty;
        //    switch (state)
        //    {
        //        case NoBotState.InvalidAddressTooActive:
        //            errorCode = "E0002";
        //            break;
        //        case NoBotState.InvalidBadResponse:
        //            errorCode = "E0003";
        //            break;
        //        case NoBotState.InvalidBadSession:
        //            errorCode = "E0004";
        //            break;
        //        case NoBotState.InvalidResponseTooSoon:
        //            errorCode = "E0005";
        //            break;
        //        case NoBotState.InvalidUnknown:
        //            errorCode = "E0006";
        //            break;
        //    }
        //    IMessageService service = ServiceFactory.Produce<IMessageService>();
        //    string errorMessage = service.Get(errorCode, Config.DefaultLanguage);
        //    return errorMessage;
        //}
    }
}
